<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>商品管理</el-breadcrumb-item>
        <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
        <!-- 搜索区域 -->
        <!-- :gutter 按钮和搜索框之间的距离 -->
        <el-row :gutter="20">
            <el-col :span="9">
                <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getGoodList">
                    <el-button slot="append" icon="el-icon-search" @click="getGoodList"></el-button>
                </el-input>
            </el-col>
            <el-col :span="4">
                <el-button type="primary" @click="goAddPage">添加商品</el-button>
            </el-col>
        </el-row>  

        <!-- 列表区域   -->
        <el-table :data="goodList" border stripe>
                <!-- 索引列 -->
                <el-table-column type="index"></el-table-column>
                <el-table-column prop="goods_name" label="商品名称"></el-table-column>
                <el-table-column prop="goods_price" label="商品价格(元)" width="95px"></el-table-column>
                <el-table-column prop="goods_weight" label="商品重量" width="70px"></el-table-column>
                <el-table-column prop="add_time" label="创建时间" width="140px">
                    <template v-slot="scope">
                        {{scope.row.add_time | dataFormat}}
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="130px">
                    <!-- eslint-disable-next-line -->
                    <template v-slot="scope">
                        <!-- 修改按钮 -->
                        <el-button type="primary" icon="el-icon-edit" size="mini" @click="editGoodList(scope.row.goods_id)"></el-button>
                        <!-- 删除按钮 -->
                        <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeGoodList(scope.row.goods_id)"></el-button>
                    </template>
                </el-table-column>    
        </el-table>   

        <!-- 分页功能 -->
        <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="queryInfo.pagenum"
            :page-sizes="[5,10, 15, 20]"
            :page-size="queryInfo.pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total" background>
        </el-pagination>    
    </el-card>    


    <!-- 修改商品对话框 -->
    <el-dialog
        title="修改商品信息"
        :visible.sync="editDialogVisible"
        width="50%">
        <el-form :model="editListData" :rules="editRules" ref="editFormRes" label-width="100px">
            <el-form-item label="商品ID：">
                <el-input v-model="editListData.goods_id" disabled></el-input>
            </el-form-item>
            <el-form-item label="商品名称：" prop="goods_name">
                <el-input v-model="editListData.goods_name"></el-input>
            </el-form-item>
            <el-form-item label="价格：" prop="goods_price">
                <el-input v-model="editListData.goods_price"></el-input>
            </el-form-item>
            <el-form-item label="数量：" prop="goods_number">
                <el-input v-model="editListData.goods_number"></el-input>
            </el-form-item>
            <el-form-item label="重量：" prop="goods_weight">
                <el-input v-model="editListData.goods_weight"></el-input>
            </el-form-item>
        </el-form>    
        <span slot="footer" class="dialog-footer">
                <el-button @click="editDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="editParams">确 定</el-button>
        </span>
    </el-dialog>    


  </div>
</template>

<script>
export default {
    name:'List',
    data() {
        return {
            // 列表数据
            goodList:[],
            queryInfo:{
                // 当前页码
                pagenum:1,
                // 每页显示的数量
                pagesize:10,
                //查询参数
                query:''
            },
            // 数据总条数
            total:0,
            // 根据id获取到的数
            editListData: {},
            // 修改商品信息对话框
            editDialogVisible: false,
            // 验证规则
            editRules: {
                goods_name:[
                    { required: true, message: '请输入商品名称', trigger: 'blur' },
                    { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
                ],
                goods_price:[
                    { required: true, message: '请输入商品价格', trigger: 'blur' },
                ],
                goods_weight:[
                    { required: true, message: '请输入商品重量', trigger: 'blur' },
                ],
                goods_number:[
                    { required: true, message: '请输入商品数量', trigger: 'blur' },
                ],
            }
        }
    },
    created() {
        // 获取商品列表
        this.getGoodList()
    },
    methods: {
        // 获取商品列表
        async getGoodList(){
            const {data: res} = await this.$http.get('goods',{params:this.queryInfo})
            // console.log(res);
            if(res.meta.status !== 200) return this.$message.error('请求商品列表数据失败！')
            // this.$message.success('请求商品列表数据成功！')
            this.goodList = res.data.goods
            this.total = res.data.total
        },
        // 监听每页显示的数量
        handleSizeChange(newSize){
            this.queryInfo.pagesize = newSize
            this.getGoodList()
        },
        // 监听当前页码发生变化
        handleCurrentChange(newPage){
            this.queryInfo.pagenum = newPage
            this.getGoodList()
        },
        // 删除按钮功能
        async removeGoodList(id){
            const removeConfrim = await this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'}).catch(err=>err) //捕获用户取消的操作
            if(removeConfrim !== 'confirm') return this.$message.info('已取消删除！')
            const {data: res} = await this.$http.delete(`goods/${id}`) 
            // console.log(res);
            if(res.meta.status !== 200) return this.$message.error('删除数据失败！')
            this.$message.success('已删除该数据')
            this.getGoodList()   
        },
        // 添加用户
        goAddPage(){
            this.$router.push('/goods/add')
        },
        // 修改商品信息
        async editGoodList(id){
            const {data: res} = await this.$http.get(`goods/${id}`)
            // console.log(res);
            if(res.meta.status !== 200) return this.$message.error('获取数据失败！')
            this.$message.success('获取数据成功！')
            this.editListData = res.data
            this.editDialogVisible = true
        },
        // 修改商品确定按钮
        editParams(){
            this.$refs.editFormRes.validate(async valid => {
                if(!valid) return
                const {data: res} = await this.$http.put(`goods/${this.editListData.goods_id}`,this.editListData)
                console.log(res);
                if(res.meta.status !== 200) return this.$message.error('修改数据失败！')
                this.$message.success('修改数据成功！')
                this.getGoodList()
                this.editDialogVisible = false
            })
        }
    },
}
</script>

<style scoped>
    
</style>